package com.feihong.yw189.bluetooth.utils.djlm.newDJMLReader;

import android.util.Base64;

import java.io.IOException;
import java.util.Arrays;

/**
 * Created by Admin on 2017/4/8.
 */
public class DJLM_IDCardBaseInfo {

    public byte[] m_data;
    public byte[] m_SW;
    public String m_strErrorInfo;
    public int m_nErrorCode;

    DJLM_IDCardBaseInfo() {
        m_nErrorCode = -1;
    }

    String GetResultText() {
        String strResultInfo = "NULL";
        if (m_nErrorCode == 0)
            strResultInfo = GetInfo(m_data);
        else if (m_strErrorInfo != null && !m_strErrorInfo.isEmpty())
            strResultInfo = m_strErrorInfo;
        return strResultInfo;
    }

    String GetErrorInfo() {
        return m_strErrorInfo;
    }

    String GetWLTBase64() {
        if (m_data != null)
            return GetWLTBase64(m_data);
        return null;
    }

    static String GetNationDescribe(int nNationCode) {
        String[] strList = new String[]{"汉族", "蒙古族", "回族", "藏族", "维吾尔族", "苗族", "彝族", "壮族", "布依族", "朝鲜族",
                "满族", "侗族", "瑶族", "白族", "土家族", "哈尼族", "哈萨克族", "傣族", "黎族", "傈僳族",
                "佤族", "畲族", "高山族", "拉祜族", "水族", "东乡族", "纳西族", "景颇族", "柯尔克孜族", "土族",
                "达翰尔族", "仫佬族", "羌族", "布朗族", "撒拉族", "毛南族", "仡佬族", "锡伯族", "阿昌族", "普米族",
                "塔吉克族", "怒族", "乌孜别克族", "俄罗斯族", "鄂温克族", "德昂族", "保安族", "裕固族", "京族", "塔塔尔族",
                "独龙族", "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族", "其它"};
        String strRet = "外国人入籍";
        if (nNationCode >= 1 && nNationCode <= 57)
            strRet = strList[nNationCode - 1];
        return strRet;
    }

    public static String GetName(byte[] data) {
        byte[] Name = new byte[30];
        try {
            System.arraycopy(data, 4, Name, 0, 30);
            String strName = new String(Name, "UTF-16LE");
            return strName;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static String GetSex(byte[] data) {
        byte[] Sex = new byte[2];
        try {
            System.arraycopy(data, 4 + 30, Sex, 0, 2);
            String strSex = new String(Sex, "UTF-16LE");
            if (strSex.equals("1"))
                strSex = "男";
            else
                strSex = "女";
            return strSex;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static String GetNation(byte[] data) {
        byte[] nationCode = new byte[4];
        try {
            System.arraycopy(data, 4 + 32, nationCode, 0, 4);
            String strNationCode = new String(nationCode, "UTF-16LE");
            strNationCode = GetNationDescribe(Integer.parseInt(strNationCode));
            return strNationCode;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static String GetBirth(byte[] data) {
        byte[] birth = new byte[16];
        try {
            System.arraycopy(data, 4 + 36, birth, 0, 16);
            String strBirth = new String(birth, "UTF-16LE");
            strBirth = strBirth.substring(0, 4) + "年" + strBirth.substring(4, 6) + "月" + strBirth.substring(6, 8) + "日";
            return strBirth;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static String GetAddress(byte[] data) {
        byte[] Address = new byte[70];
        try {
            System.arraycopy(data, 4 + 52, Address, 0, 70);
            String strAddress = new String(Address, "UTF-16LE");
            return strAddress;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static String GetIDCode(byte[] data) {
        byte[] ID = new byte[36];
        try {
            System.arraycopy(data, 4 + 122, ID, 0, 36);
            String strID = new String(ID, "UTF-16LE");
            return strID;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static String GetRegorg(byte[] data) {
        byte[] Regorg = new byte[30];
        try {
            System.arraycopy(data, 4 + 158, Regorg, 0, 30);
            String strRegorg = new String(Regorg, "UTF-16LE");
            return strRegorg;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static String GetStartDate(byte[] data) {
        byte[] startDate = new byte[16];
        try {
            System.arraycopy(data, 4 + 188, startDate, 0, 16);
            String strStartDate = new String(startDate, "UTF-16LE");
            strStartDate = strStartDate.substring(0, 4) + "年" + strStartDate.substring(4, 6) + "月" + strStartDate.substring(6, 8) + "日";
            return strStartDate;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static String GetEndDate(byte[] data) {
        byte[] endDate = new byte[16];
        try {
            System.arraycopy(data, 4 + 204, endDate, 0, 16);
            String strEndDate = new String(endDate, "UTF-16LE");
            if (strEndDate.contains("长期")) return strEndDate.trim();
            strEndDate = strEndDate.substring(0, 4) + "年" + strEndDate.substring(4, 6) + "月" + strEndDate.substring(6, 8) + "日";
            return strEndDate;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] GetWLTData(byte[] data) {
        byte[] wltData = Arrays.copyOfRange(data, 4 + 256, 4 + 256 + 1024);
        return wltData;
    }

    public byte[] GetWLTByte() {
        if (m_data != null)
            return GetWLTData(m_data);
        return null;
    }

    public static String GetWLTBase64(byte[] data) {
        byte[] wltData = GetWLTData(data);
        String strBase64 = Base64.encodeToString(wltData, Base64.DEFAULT);
        return strBase64;
    }

    public static String GetJSON(byte[] data) {
        String strInfo;
        strInfo = "{\"Name\":\"" + GetName(data) + "\""
                + ",\"Sex\":\"" + GetSex(data) + "\""
                + ",\"Nation\":\"" + GetNation(data) + "\""
                + ",\"Born\":\"" + GetBirth(data) + "\""
                + ",\"Address\":\"" + GetAddress(data) + "\""
                + ",\"CardNo\":\"" + GetIDCode(data) + "\""
                + ",\"Police\":\"" + GetRegorg(data) + "\""
                + ",\"ActivityLFrom\":\"" + GetStartDate(data) + "\""
                + ",\"ActivityLTo\":\"" + GetEndDate(data) + "\"}";
        //log.info(strInfo);
        return strInfo;
    }

    public static String GetJSON(String strBase64) {
        byte[] data = null;
        data = Base64.decode(strBase64, Base64.DEFAULT);
        String strInfo = null;
        if (data != null) {
            if (data.length >= 4 + 256)
                strInfo = GetJSON(data);
            else if (data.length == 256) {
                byte[] buf = new byte[4 + 256];
                Arrays.fill(buf, (byte) 0);
                System.arraycopy(data, 0, buf, 4, 256);
                strInfo = GetJSON(buf);
            }
        }
        return strInfo;
    }

    public static String GetInfo(byte[] data) {
        String strInfo;

        strInfo = "姓名:" + GetName(data)
                + "性别:" + GetSex(data)
                + "民族:" + GetNation(data)
                + "出生:" + GetBirth(data)
                + "住址:" + GetAddress(data)
                + "公民身份号码:" + GetIDCode(data)
                + "签发机关:" + GetRegorg(data)
                + "有效期限:" + GetStartDate(data)
                + "-" + GetEndDate(data);
        return strInfo;
    }

}
